package com.mentaleye.biz.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.CurriculumTempBean;
import com.mentaleye.biz.CurriculumTempBiz;
import com.mentaleye.dal.model.CurriculumTempExample;
import com.mentaleye.dal.model.CurriculumTempExample.Criteria;
import com.mentaleye.service.CurriculumTempService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;

@Service
public class CurriculumTempBizImpl implements CurriculumTempBiz {

	@Autowired private CurriculumTempService curriculumTempService;
	private static Logger logger = Logger.getLogger(CurriculumTempBizImpl.class);
	
	public List<CurriculumTempBean> queryCurriculumTemp(String curriculumTempName, Integer curriclumTempType,
			Integer curriclumTempStatus) {
		try{
		CurriculumTempExample example = new CurriculumTempExample();
		Criteria cri = example.createCriteria();
		if(StringUtils.isNotEmpty(curriculumTempName)){
			cri.andCurriculumTempNameLike(curriculumTempName);
		}
		if(curriclumTempType != null){
			cri.andCurriculumTempTypeEqualTo(curriclumTempType);
		}
		if(curriclumTempStatus != null){
			cri.andCurriculumTempStatusEqualTo(curriclumTempStatus);
		}
		return curriculumTempService.queryCurriculumTemp(example);
		}catch (Exception e){
			logger.error("课程模版查询异常",e);
			return null;
		}
	}

	public CommonResult editCurriculumTemp(CurriculumTempBean curriclumTemp) {
		CommonResult result = new CommonResult();
		boolean add;
		boolean success =true;
		add = curriclumTemp.getCurriculumTempNo()==null;
		try{
			success = curriculumTempService.editCurriculumTemp(curriclumTemp, add);
		}catch(Exception e){
			logger.error("编辑模版异常", e);
			success = false;
		}
		result.setIsSuccess(success);
		return result;
	}

	public void queryCurriculumTempByPage(CommonPage commonPage) {
		try{
		curriculumTempService.queryCurriculumTempByPage(commonPage);
		}catch (Exception e){
			logger.error("查询课程模版分页异常",e);
		}
	}

	public CommonResult deleteCurriculumTemp(List<Integer> ids) {
		CommonResult commonResult=new CommonResult();
		boolean success=false;
		try {
			for (Integer id : ids) {
			CurriculumTempBean curriculumTempBean=curriculumTempService.getCurriculumTempByNo(id);
			curriculumTempBean.setCurriculumTempStatus(142);
			success=curriculumTempService.editCurriculumTemp(curriculumTempBean, false);
			}
			
		} catch (Exception e) {
			success=false;
		}
		commonResult.setIsSuccess(success);
		return commonResult;
	}

	@SuppressWarnings("unchecked")
	public boolean saveRlate(Map<String, Object> ids) {
		try{
		Map<Integer,Integer> delete=(Map<Integer, Integer>) ids.get("delete");
		Integer relateTempNo=Integer.valueOf((String) ids.get("relateTempNo")) ;
		Map<Integer,Integer> add=(Map<Integer, Integer>) ids.get("add");
		if(add != null && add.size()>0){
			for(Integer key:add.keySet()){
				curriculumTempService.saveRlate(key, relateTempNo, null);
			}
		}
		List<Integer> deletes = new ArrayList<Integer>();
		if(delete != null && delete.size()>0){
			for(Integer key:delete.keySet()){
				deletes.add(key);
			}
			curriculumTempService.deleteRlate(deletes, relateTempNo);
		}
		return true;
		}catch(Exception e){
			logger.error("保存模版关联异常",e);
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	public CommonResult saveHardRlate(Map<String, Object> ids) {
		CommonResult commonResult=new CommonResult();
		boolean success=false;
		
		try{
		Map<Integer, Map<Integer,Object>> delete = (Map<Integer, Map<Integer, Object>>) ids.get("delete");
		Integer relateTempNo=Integer.valueOf(ids.get("relateTempNo").toString()) ;
		Map<Integer, Map<Integer,Object>> add = (Map<Integer, Map<Integer, Object>>) ids.get("add");
		Map<String, Map<Integer,Object>> update = (Map<String, Map<Integer, Object>>) ids.get("update");
		if(add != null && add.size()>0){
			for (Entry<Integer, Map<Integer,Object>> entry : add.entrySet()) {
				int a =Integer.valueOf(entry.getValue().get("sort").toString());
				Integer b = Integer.valueOf(entry.getValue().get("cNo").toString());
				success = curriculumTempService.saveRlate(b, relateTempNo,a);
			}
		}
		if(update != null && update.size()>0){
			for(Entry<String, Map<Integer, Object>> entry:update.entrySet()){
				int a =Integer.valueOf(entry.getValue().get("sort").toString());
				Integer b = Integer.valueOf(entry.getKey());
				System.out.print(b);
				success = curriculumTempService.updateRlate(b, relateTempNo, a);
			}
		}
		List<Integer> deletes = new ArrayList<Integer>();
		
		if(delete != null && delete.size()>0){
			System.out.println(delete.size());
			for(Object key:delete.values()){
			System.out.println(key.getClass());
			}
			for(Entry<Integer, Map<Integer,Object>> key:delete.entrySet()){
				deletes.add(key.getKey());
			}
			success=curriculumTempService.deleteRlate(deletes, relateTempNo);
		}
		
		}catch(Exception e){
			logger.error("saveHardRlate错误异常",e);
			
		}
		commonResult.setIsSuccess(success);
		return commonResult;
	}

	public CurriculumTempBean getCurriculumTempByNo(Integer curriculumTempNo) {
		try{
			return curriculumTempService.getCurriculumTempByNo(curriculumTempNo);
		}catch(Exception e){
			logger.error("根据模版编号查询异常",e);
			return null;
		}

	}
	
	public List<CurriculumTempBean> queryByExample(CurriculumTempExample example) {
		try{
			return curriculumTempService.queryByExample(example);
		}catch(Exception e){
			logger.error("根据条件查询异常",e);
			return null;
		}

	}

}
